package com.zb.frame.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zb.frame.base.model.MPPage;
import com.zb.frame.modules.sys.entity.Role;
import com.zb.frame.modules.sys.mapper.RoleMapper;
import com.zb.frame.modules.sys.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bangZZ
 * @since 2020-06-04
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    /**
     * 获取角色列表 分页
     * @param params
     * @return
     */
    @Override
    public MPPage getRoleList(Page<Role> pageInfo, Map<String, Object> params) {
        QueryWrapper<Role> wrapper = new QueryWrapper();
        wrapper.ne("role_id","ADMIN"); // 过滤掉超级管理员
        if (StringUtils.isNotBlank((String)params.get("name"))) {
            wrapper.and(qw -> qw.like("role_id", params.get("name")).or().like("role_name", params.get("name")));
        }
        wrapper.orderByDesc("create_time");
        IPage<Role> page = this.page(pageInfo, wrapper);
        return new MPPage(page);
    }

    @Override
    public boolean isExist(String roleId) {
        QueryWrapper<Role> wrapper = new QueryWrapper<Role>();
        wrapper.eq("role_id", roleId);
        return this.count(wrapper) > 0;
}

    @Override
    public Role addRole(Role data) {
        Role role = new Role();
        role.setRoleId(data.getRoleId().toUpperCase());
        role.setRoleName(data.getRoleName());
        role.setRoleDesc(data.getRoleDesc());
        role.setIsSys(0);
        return this.save(role) ? role : null;
    }

    @Override
    public Role updateRole(Role data) {
        Role role = new Role();
        role.setRoleId(data.getRoleId().toUpperCase());
        role.setRoleName(data.getRoleName());
        role.setRoleDesc(data.getRoleDesc());
        boolean f = this.updateById(role);
        return f ? role : null;
    }
}
